<template>
  <div class="xtx-confirm" :class="{fade}">
    <div class="wrapper" :class="{fade}">
      <div class="header">
        <h3>{{title}}</h3>
        <a @click="cancel" href="javascript:;" class="iconfont icon-close-new"></a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{text}}</span>
      </div>
      <div class="footer">
        <XtxButton @click="cancel" size="mini" type="gary" style="margin-right: 10px;">取消</XtxButton>
        <XtxButton @click="submit" size="mini" type="primary">确认</XtxButton>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { onMounted } from '@vue/runtime-core'
import XtxButton from './xtx-button.vue'

export default {
  name: 'XtxConfirm',
  props: {
    title: {
      type: String,
      default: '温馨提示'
    },
    text: {
      type: String,
      default: ''
    },
    // props也可以传递函数（子组件可以使用父组件传递进来的函数）
    cancelCallback: {
      type: Function
    },
    submitCallback: {
      type: Function
    }
  },
  setup (props) {
    // 默认对话框不展示
    const fade = ref(false)
    // 组件渲染完毕时展示
    onMounted(() => {
      // 过渡效果需要在元素创建完毕后延时一会儿加上才会触发
      setTimeout(() => {
        fade.value = true
      }, 0)
    })
    // 取消
    const cancel = () => {
      props.cancelCallback()
    }
    // 确认
    const submit = () => {
      props.submitCallback()
    }
    return { fade, cancel, submit }
  },
  components: { XtxButton }
}
</script>

<style scoped lang="less">
.xtx-confirm{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background-color: rgba(0,0,0,0);
  &.fade{
    transition: all 1s;
    background: rgba(0,0,0,0.5);
  }
  .wrapper{
    width: 400px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-60%);
    opacity: 0;
    &.fade{
      transition: all 0.4s;
      transform: translate(-50%,-50%);
      opacity: 1;
    }
    .header,.footer{
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
      a{
        position: absolute;
        right: 0;
        top: 0;
        padding-right: 15px;
      }
    }
    .body{
      padding: 15px 40px;
      i{
        color:red;
      }
      span{
        margin-left: 5px;
      }
    }
    .footer{
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
